/*
 * Copyright (c) 2011. This file is public under GPLv3 license.
 * For more information about license please refer to http://www.gnu.org/licenses/gpl.html
 */

package com.freetools.watchdog.tests;

import junit.framework.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Date: Mar 2, 2011
 *
 * @author Michael Morozov
 */
public class Experiments
{
    @Test
    public void testStringSortByHashCode()
    {
        String[] testArray = new String[]{"AQdd", "W", "Z", "AW", "AA", "A", "10"};
        int[] testArrayHashes = new int[testArray.length];
        for (int i = 0; i < testArray.length; i++) testArrayHashes[i] = testArray[i].hashCode();
        Arrays.sort(testArrayHashes);
        for (int testArrayHash : testArrayHashes)
            for (String aTestArray : testArray)
                if (testArrayHash == aTestArray.hashCode())
                {
                    System.out.println(testArrayHash);
                    System.out.println(aTestArray);
                }


    }

    @Test
    public void testLong()
    {
        long a = -10;
        long b = 20;
        System.out.println(a - b);
    }

    @Test
    public void testGroupCount()
    {
        Pattern p = Pattern.compile("(\\d{2})(\\w{2})(\\d{2})");
        Matcher matcher = p.matcher("12aa34");
        if (!matcher.find())
        {
            System.out.println("Nothing was found");
            return;
        }
        Assert.assertEquals(3, matcher.groupCount());
        MatchResult matchResult = matcher.toMatchResult();
        System.out.println("Group Count:" + matchResult.groupCount());
        System.out.println("Group 1:" + matchResult.group(1));
    }

    @Test
    public void testOr()
    {
        Pattern intFormat = Pattern.compile("int|id", Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = intFormat.matcher("yuiw_int");
        Matcher matcher2 = intFormat.matcher("yuiw_i");
        if (!matcher1.find())
        {
            System.out.println("Matcher 1: Nothing was found");
            return;
        }
        if (!matcher2.find())
        {
            System.out.println("Matcher 2: Nothing was found");
            return;
        }
        System.out.println("It is ok");
    }

    @Test
    public void testReplace()
    {
        Pattern intFormat = Pattern.compile("select (.*)\\s?from", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
        StringBuilder s = new StringBuilder("select iuyiwkkshfdksjdlksjfl\n from where a = 1");
        Matcher matcher1 = intFormat.matcher(s);
        if (matcher1.find())
        {
            int start = matcher1.start(1);
            s.delete(start, matcher1.end(1)).insert(start, "count(1)");
        }
        System.out.println(s);
    }
}
